<template>
  <el-dialog v-model="drawerVisible" :destroy-on-close="true" size="450px" width="750px" :title="`${drawerProps.title}用户`">
    <el-form
      class="form"
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :inline="true"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="drawerProps.row!.userName" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="drawerProps.row!.account" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="mobile">
        <el-input v-model="drawerProps.row!.mobile" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="drawerProps.row!.email" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="drawerProps.row!.password" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="drawerProps.row!.userType" placeholder="" clearable>
          <el-option v-for="item in userTypeList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue + ''" />
        </el-select>
      </el-form-item>
      <el-form-item label="岗位" prop="postCode">
        <el-cascader
          :model-value="drawerProps.row!.postCode"
          @change="
            (e: any) => {
              drawerProps.row!.postCode = e[0];
            }
          "
          :options="drawerProps.postOption"
          :props="{ label: 'postName', value: 'postCode', checkStrictly: true }"
          clearable
        />
      </el-form-item>
      <el-form-item label="角色" prop="roleId">
        <el-cascader
          :model-value="drawerProps.row!.roleId"
          @change="
            (e: any) => {
              drawerProps.row!.roleId = e[0];
            }
          "
          :options="drawerProps.roleOption"
          :props="{ label: 'roleName', value: 'id', checkStrictly: true }"
          clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts" name="accountDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { System } from "@/api/interface";
import { mobile, email } from "@/utils/rules";
const rules = reactive({
  userName: [{ required: true, message: "请填写用户名" }],
  account: [{ required: true, message: "请填写账号" }],
  mobile: [
    { required: true, message: "请填写电话" },
    {
      validator: mobile,
      message: "请填写正确的电话",
      trigger: ["blur", "change"]
    }
  ],
  email: [
    { required: true, message: "请填写邮箱" },
    {
      validator: email,
      message: "请填写正确的邮箱",
      trigger: ["blur", "change"]
    }
  ]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  postOption?: System.ResPostList[];
  roleOption?: System.ResRoleList[];
  row: Partial<System.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  enumMap?: any;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});
const userTypeList = ref<System.ResDictList[]>([]);
// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  userTypeList.value = params.enumMap.get("userType");
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
<style lang="scss" scoped>
.form {
  :deep(.el-input) {
    --el-input-width: 220px;
  }
}
</style>
